package com.putable.nmc.dmv;

import com.putable.nmc.world.CIT;
import com.putable.nmc.world.CustomerType;
import com.putable.nmc.world.World;
import com.putable.pqueue.PQueue;

public class Customer extends CLineAble {
	
	
	public Customer(){
	}
	public Customer(PQueue pq, CIT cit, World world){
		super(pq, cit, world);
		this.setState(CustomerState.ARRIVED);
		this.setType(cit.getType());
		maxCapacity = world.getDMVCapacity();
	}
	
	@Override
	public String toString() {
		return "Customer [arrival time " + (int)getCit().getArrivalTime() + ", getState()=" + getState()
				+ ", getTimeOfNextTask()=" + (int)getTimeOfNextTask() + ", getEntered()=" + getCit().getEntered()+"]";
	}
	
	private int maxCapacity;
	
	/**
	 * @param type The type of customer this is
	 */
	private CustType type;
	@Override
	public CustType getType() {
		return type;
	}
	@Override
	public void setType(CustomerType type) {
		if(type.toString() == CustType.RETIREE.toString()){
			this.type = CustType.RETIREE;
		}else if(type.toString() == CustType.WORKER.toString()){
			this.type = CustType.WORKER;
		}else if(type.toString() == CustType.STUDENT.toString()){
			this.type = CustType.STUDENT;
		}else if(type.toString() == CustType.BIKER.toString()){
			this.type = CustType.BIKER;
		}
	}

	@Override
	public void run(double time, CustomerLine cLine) {
		if(this.getState() == CustomerState.WAITING){
			cLine.delete(this, time);
			getWorld().exitInterview(getCit());
		}else if(this.getState() == CustomerState.TRANSACTING){
			getCit().setTaskCompleteAt(time);
			getWorld().exitInterview(getCit());
		}else if(this.getState() == CustomerState.ARRIVED){
			if(cLine.getSize() < maxCapacity){
	            getCit().setEntered(true);
				setState(CustomerState.WAITING);
				this.setTimeOfNextTask(time+this.getCit().getGiveupTime());
				this.getPQueue().insert(this);
				cLine.add(this, time);
			}else{
				
				getCit().setEntered(false);
				this.getWorld().exitInterview(getCit());
			}
		}
	}
	
}
